import request from '@api/request';
import { HttpUrl } from '@api/httpUrl';
import { Column } from 'element-plus';
import { useUserStore } from '@/store/modules/user';

// 获取采购列表
export function getPurchaseList(available: boolean, searchPurchaseValue: string, page: number, pageSize: number) {
    return request.get<AjaxResponse['purchaseList']>(HttpUrl.getPurchaseList, { params: { available, searchPurchaseValue, page, pageSize } });
}

// 风险评估
export function getPurchaseRiskAssessment() {
    return request.get<AjaxResponse['purchaseList']>(HttpUrl.getPurchaseRiskAssessment);
}

// 创建采购单
export function createPurchase(skipPurchaseValidate: boolean, purchaseForm: AjaxRequest['purchaseForm']) {
    return request.post(HttpUrl.createPurchase, { skipPurchaseValidate, ...purchaseForm });
}

// 创建产品采购
export function productPurchase(purchaseForm: AjaxRequest['purchaseForm']) {
    return request.post(HttpUrl.productPurchase, { ...purchaseForm });
}

// 转库存纸
export function transferStock(selected: string, creator: string = useUserStore().username, creatorId: string = useUserStore().id) {
    return request.get(HttpUrl.transferStock, { params: { selected, creator, creatorId } });
}

// 库存纸转采购
export function transferPurchase(stockId: string, stockNumber: number, distributionData: AjaxRequest['distributionData']) {
    return request.post(HttpUrl.transferPurchase, { stockId, stockNumber, distributionData });
}

// 手动跳过采购单控制凭据
export function skipProcurementControlReceipts(purchaseId: string) {
    return request.put(HttpUrl.skipProcurementControlReceipts, { purchaseId });
}

// 删除采购凭证
export function deleteProcurementControlReceipts(purchaseId: string) {
    return request.put(HttpUrl.deleteProcurementControlReceipts, { purchaseId });
}

// 更新采购单
export function updatePurchase(purchaseId: string, field: Column['dataKey'], value: any) {
    return request.put(HttpUrl.updatePurchase, { purchaseId, field, value });
}

// 上传单据信息
export function uploadProcurementControlInfo(
    procurementControlInfoId: string,
    procurementControlInfo: { arrivalQuantity: number; arrivalAmount: number }
) {
    return request.post(HttpUrl.uploadProcurementControlInfo, { procurementControlInfoId, procurementControlInfo });
}

// 更新订单打印生产单标识
export function updateOrderPrintFlag(ids: string[]) {
    return request.put(HttpUrl.updateOrderPrintFlag, { ids });
}

// 删除采购单
export function deletePurchase(purchaseId: string[]) {
    return request.put(HttpUrl.deletePurchase, { purchaseId });
}

// 恢复采购单
export function recoverPurchase(purchaseId: string[]) {
    return request.put(HttpUrl.recoverPurchase, { purchaseId });
}

// 移除采购单
export function removePurchase(purchaseId: string[]) {
    return request.put(HttpUrl.removePurchase, { purchaseId });
}
